$(function() {

    //定义补0函数
    function padZero(n) {
        return n > 10 ? n : '0' + 0
    }

    //格式化事件
    template.defaults.imports.formatDate = function(dates) {
        var dt = new Date(dates)
        var y = dt.getFullYear(),
            m = padZero(dt.getMonth() + 1),
            d = padZero(dt.getDate()),
            hh = padZero(dt.getHours()),
            mm = padZero(dt.getMinutes()),
            ss = padZero(dt.getSeconds())
        return y + '-' + m + '-' + d + '  ' + hh + ':' + mm + ':' + ss
    }

    //渲染评论列表
    function renderComment() {
        $.ajax({
            method: 'get',
            url: 'admin/comments',
            success: function(res) {
                var htmlStr = template('table-tpl', res)
                $('tbody').html(htmlStr)
            }
        })
    }
    renderComment()

    $('tbody').on('click', '.delete', function() {
        var id = $(this).attr('data-id')
        layer.confirm('确认删除?', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                method: 'DELETE',
                url: 'admin/comments/' + id,
                success: function(res) {
                    if (res.status !== 0) return layer.msg(res.message)
                    layer.msg(res.message)
                    renderComment()
                }
            })

            layer.close(index)
        })
    })

})